//
//  OndDayViewController.m
//  news
//
//  Created by imacbookair on 16/3/14.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "OndDayViewController.h"
#import "OneDayDetailViewController.h"


@interface OndDayViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *oneDayTableView;
@property (nonatomic, strong)NSMutableArray *dataArr;

@end

@implementation OndDayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBarController.tabBar.hidden = YES;
    [self tableView];
    [self data];
}


#pragma mark - 协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OneDayModel *one = _dataArr[indexPath.row];
    
    NSMutableArray *arr = [one.imgxtra mutableCopy];
    // 判断首位cell
    if (indexPath.row == 0) {
        OneDayThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneDayThreeTableViewCell" forIndexPath:indexPath];
        cell.title.text = one.title;
        cell.content.text = one.digest;
        cell.count.text = [one.votecount.stringValue stringByAppendingString:@"跟帖"];
        [cell.picture sd_setImageWithURL:[NSURL URLWithString:one.imgsrc]];
        
        
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
        
    }
    else if (arr.count == 0){
        OneDayOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneDayOneTableViewCell" forIndexPath:indexPath];
        cell.title.text = one.title;
        cell.content.text = one.digest;
        cell.contentCount.text = [one.votecount.stringValue stringByAppendingString:@"跟帖"];
        [cell.picture sd_setImageWithURL:[NSURL URLWithString:one.imgsrc]];
        
        [cell NightWithType:UIViewColorTypeNormal];
        
        return cell;
    }
    else{
        OneDayTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneDayTwoTableViewCell" forIndexPath:indexPath];
        cell.title.text = one.title;
        cell.count.text = [one.votecount.stringValue stringByAppendingString:@"跟帖"];
        [cell.leftPicture sd_setImageWithURL:[NSURL URLWithString:one.imgsrc]];
        [cell.amongPicture sd_setImageWithURL:arr[0][@"imgsrc"]];
        [cell.rightPicture sd_setImageWithURL:arr[0][@"imgsrc"]];
        
        [cell NightWithType:UIViewColorTypeNormal];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 根据图片个数 确定cell的大小
    OneDayModel *one = _dataArr[indexPath.row];
    NSArray *arr = one.imgxtra;
    if (indexPath.row == 0) {
        return 340 * KHIGHT;
    }else if (arr.count == 0){
        return 130 * KHIGHT;
    }else{
        return 160 * KHIGHT;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OneDayModel *one = _dataArr[indexPath.row];
    OneDayDetailViewController *oneVC = [[OneDayDetailViewController alloc]init];
    oneVC.url = one.url;
    [self.navigationController pushViewController:oneVC animated:YES];
    
}
#pragma mark - 私有方法
// 创建tableView
- (void)tableView
{
    self.oneDayTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.oneDayTableView.delegate = self;
    _oneDayTableView.dataSource = self;
    _oneDayTableView.showsVerticalScrollIndicator = NO;
    _oneDayTableView.contentInset = UIEdgeInsetsMake(0, 0, 120 * KWITH, 0);
    
    [_oneDayTableView registerClass:[OneDayOneTableViewCell class] forCellReuseIdentifier:@"OneDayOneTableViewCell"];
    [_oneDayTableView registerClass:[OneDayTwoTableViewCell class] forCellReuseIdentifier:@"OneDayTwoTableViewCell"];
    [_oneDayTableView registerClass:[OneDayThreeTableViewCell class] forCellReuseIdentifier:@"OneDayThreeTableViewCell"];
    
    [self.view addSubview:_oneDayTableView];
    
    
}

- (void)data
{
    NSString *str = @"http://c.3g.163.com/nc/article/list/T1429173762551/0-20.html";
    [AFNetBlock GetDataWithURL:str block:^(id block) {
        self.dataArr = [OneDayModel modelHanderWithArray:block[@"T1429173762551"]];
        [_oneDayTableView reloadData];
    }];
    
}
#pragma mark - 按钮方法
- (void)back:(UIBarButtonItem *)back
{
    NSLog(@"back");
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
